﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using ZenSystemService.Api.Business;
using ZenSystemService.Api.Common.Auth;
using ZenSystemService.Api.DTOs;
using ZenSystemService.Api.DTOs.ProductType;
using ZenSystemService.Api.Entities;
using ZenSystemService.Api.Services;

namespace ZenSystemService.Api.Controllers
{
    /// <summary>
    ///产品类型接口
    /// </summary>
    [Route("api/productTypes")]
    [ApiController]
    public class ProductTypeController : ControllerBase
    {
        private readonly ProductTypeService _productTypeService;

        public ProductTypeController(ProductTypeService productTypeService)
        {
            _productTypeService = productTypeService;
        }

        /// <summary>
        /// 得到所有产品类型
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ReturnObjectT<IEnumerable<ProductTypeQueryResultDTO>>> GetAllProductTypes(ReceiveObject _receiveObject)
        {
            ReturnObjectT<IEnumerable<ProductTypeQueryResultDTO>> _returnObject = new ReturnObjectT<IEnumerable<ProductTypeQueryResultDTO>>();


            var ProductTypes = await _productTypeService.GetAllProductTypesAsync();
            _returnObject.Code = 1;
            _returnObject.Data = ProductTypes;
            return _returnObject;
            // return Ok(ProductTypes);
        }

        /// <summary>
        /// 根据ID 获取产品类型实体信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost("GetProductTypeById")]
        public async Task<ReturnObjectT<ProductTypeQueryResultDTO>> GetProductTypeById(ReceiveObjectT<int> _receiveObjectT)
        {
            ReturnObjectT<ProductTypeQueryResultDTO> _returnObject = new ReturnObjectT<ProductTypeQueryResultDTO>();

            int id = _receiveObjectT.Data;
            var ProductType = await _productTypeService.GetProductTypeByIdAsync(id);
            if (ProductType == null)
            {
                //return NotFound();
                _returnObject.Code = 0;
                _returnObject.ErrorMsg = "获取产品类型信息失败";
            }
            else
            {
                _returnObject.Code = 1;
                _returnObject.Data = ProductType;
            }
            return _returnObject;
            // return Ok(ProductType);
        }
        /// <summary>
        /// 创建产品类型信息
        /// </summary>
        /// <param name="productType"></param>
        /// <returns></returns>
        [HttpPost("CreateProductType")]
        public async Task<IActionResult> CreateProductType(ProductTypeCreateRequestDTO productType)
        {
            var productTypeResult = await _productTypeService.CreateProductTypeAsync(productType);

            return CreatedAtAction(nameof(GetProductTypeById),
                new { id = productTypeResult.ProductTypeId }, productTypeResult);
        }

        /// <summary>
        /// 更新产品类型信息
        /// </summary>
        /// <param name="id"></param>
        /// <param name="productType"></param>
        /// <returns></returns>
        [HttpPost("UpdateProductType")]
        public async Task<IActionResult> UpdateProductType(ProductTypeUpdateRequestDTO req)
        {
            var productType = await _productTypeService.UpdateProductTypeAsync(req);
            return Ok(productType);
        }
    }
}